package com.boc.image.web;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.boc.bus.control.BusOutputResult;
import com.boc.ibatis.config.IbatisSqlClient;
import com.boc.news.beans.EncryptMD5;

public class User {
	
	/**
	 * 取用户基本信息
	 * @param request
	 * @param response
	 * @param m
	 * @return
	 */
	public BusOutputResult getUserInfo(HttpServletRequest request,
			HttpServletResponse response, Map m) {
		BusOutputResult result = new BusOutputResult();
		try {
			//查看相册名称是否存在
			String userid = request.getSession().getAttribute("userId").toString();
			m.put("nUserid", userid);
			List list = IbatisSqlClient.sqlMapClient.queryForList("image_t_img_user.ibatorgenerated_selectByPrimaryKey",m);
			result.addValues("P_RESULT", list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 保存用户信息
	 * @param request
	 * @param response
	 * @param m
	 * @return
	 */
	public BusOutputResult saveUserInfo(HttpServletRequest request,
			HttpServletResponse response, Map m) {
		BusOutputResult result = new BusOutputResult();
		try {
			//查看相册名称是否存在
			String userid = request.getSession().getAttribute("userId").toString();
			m.put("nUserid", userid);
			IbatisSqlClient.sqlMapClient.update("image_t_img_user.updateUserInfo",m);
			result.addValue("PN_RET", "0");
		} catch (Exception e) {
			result.addValue("PN_RET", "-1");
			result.addValue("PC_MSG", e.getMessage());
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 上传头像照片
	 * @param request
	 * @param response
	 * @param m
	 * @return
	 */
	public BusOutputResult uploadUserHeader(HttpServletRequest request,
			HttpServletResponse response, Map m)  throws UnsupportedEncodingException , IOException{
		BusOutputResult result = new BusOutputResult();
		request.setCharacterEncoding("UTF-8");
		DiskFileItemFactory factory = new DiskFileItemFactory();
		// 设置内存缓冲区，超过后写入临时文件
		factory.setSizeThreshold(10240000);// 10M
		// 设置临时文件存储位置
		String base = request.getRealPath("/")+"images"+File.separator+"header";
		File file = new File(base);
		if (!file.exists())
			file.mkdirs();
		factory.setRepository(file);
		ServletFileUpload upload = new ServletFileUpload(factory);
		// 设置单个文件的最大上传值
		upload.setFileSizeMax(10002400000l);// 100M
		// 设置整个request的最大值
		upload.setSizeMax(10002400000l);// 1000M
		upload.setHeaderEncoding("UTF-8");
		String path = base+File.separator+m.get("nUserid")+".bin";
		try {
			m.put("cTx", m.get("nUserid")+".bin");
			IbatisSqlClient.sqlMapClient.update("image_t_img_user.ibatorgenerated_updateByPrimaryKeySelective",m);
			List<?> items = upload.parseRequest(request);
			FileItem item = null;
			for (int i = 0; i < items.size(); i++) {
				item = (FileItem) items.get(i);
				// 保存文件
				if (!item.isFormField() && item.getName().length() > 0) {
					item.write(new File(path));
				}
			}
			result.addValue("PN_RET", "0");
			
		} catch (Exception e) {
			result.addValue("PN_RET", "-1");
			result.addValue("PC_MSG", e.getMessage());
			throw new IOException();
		}
		return result;
	}
	
	/**
	 * 修改密码
	 * @param request
	 * @param response
	 * @param m
	 * @return
	 */
	public BusOutputResult saveUserPass(HttpServletRequest request,
			HttpServletResponse response, Map m) {
		BusOutputResult result = new BusOutputResult();
		try {
			//查看相册名称是否存在
			String userid = request.getSession().getAttribute("userId").toString();
			String userName = request.getSession().getAttribute("userName").toString();
			String md5 = EncryptMD5.MD5(userName+m.get("oldPass"));
			String md52 = EncryptMD5.MD5(userName+m.get("newPass"));
			m.put("nUserid", userid);
			m.put("oldpass", md5);
			m.put("cLoginpass", md52);
			Object count = IbatisSqlClient.sqlMapClient.queryForObject("image_t_img_user.getOldPass",m);
			if(Integer.parseInt(count.toString()) == 0) {
				result.addValue("PN_RET", "-1");
				result.addValue("PC_MSG", "原始密码不正确!");
				return result;
			}
			IbatisSqlClient.sqlMapClient.update("image_t_img_user.ibatorgenerated_updateByPrimaryKeySelective",m);
			result.addValue("PN_RET", "0");
		} catch (Exception e) {
			result.addValue("PN_RET", "-1");
			result.addValue("PC_MSG", e.getMessage());
			e.printStackTrace();
		}
		return result;
	}
}
